package com.ducquoc.examreviewdegreea1;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.MarshalFloat;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.AsyncTask;
import android.widget.Toast;

public class DocUpdateAsyncTask extends
		AsyncTask<ArrayList<DocPojo>, Void, ArrayList<DocPojo>> {
	ArrayList<DocPojo> arrUpdate = new ArrayList<DocPojo>();
	final String URL = "http://ltddhitc12hcb.somee.com/ducquocservice.asmx?WSDL";
	private DocLoadDataActivity activity;
	private DocDatabase database;
	private int version = 1;
	String currentDate = "", currentVersion = "", currentStatus = "null";

	public DocUpdateAsyncTask(DocLoadDataActivity activity) {
		this.activity = activity;
		database = new DocDatabase(activity);
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		Toast.makeText(activity, "Đang tải dữ liệu!", Toast.LENGTH_LONG).show();
		
		//Doc du lieu de lay version cua CSDL tu file test_database.txt
		DocReadWriteFile docRWFile = new DocReadWriteFile();
		ArrayList<String> arrTestDbs = docRWFile.ReadFile();

		// Gan du lieu co trong ArrayList vao cac bien
		currentDate = arrTestDbs.get(0).toString();
		currentVersion = arrTestDbs.get(1).toString();
		version = Integer.parseInt(currentVersion);
		currentStatus = arrTestDbs.get(2).toString();
	}

	@Override
	protected ArrayList<DocPojo> doInBackground(ArrayList<DocPojo>... arg0) {
		try {
			final String NAMESPACE = "http://tempuri.org/";
			final String METHOD_NAME = "getUpdate";
			final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.dotNet = true;
			envelope.setOutputSoapObject(request);
			// Neu truyen so thuc tren mang bat buoc phai dang ky MarshalFloat
			// neu khong se bi bao loi
			MarshalFloat marshal = new MarshalFloat();
			marshal.register(envelope);

			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
			androidHttpTransport.call(SOAP_ACTION, envelope);
			// Get Array Catalog into soapArray
			SoapObject soapArray = (SoapObject) envelope.getResponse();
			arrUpdate.clear();
			// Vong lap duyet qua tung dong du lieu
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				// (SoapObject) soapArray.getProperty(i) get item at position i
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);

				DocPojo update = new DocPojo();
				// Map dung theo ten cot tren webservices
				String _idTemp = soapItem.getProperty("id").toString();
				int _id = Integer.parseInt(_idTemp);
				update.setId(_id);
				String _versionTemp = soapItem.getProperty("version").toString();
				int _version = Integer.parseInt(_versionTemp);
				update.setVersion(_version);
				String _date = soapItem.getProperty("date").toString();
				update.setDate(_date);
				String _direct = soapItem.getProperty("direct").toString();
				update.setDirect(_direct);
			
				//Them du lieu vao bang tbl_update
				database.open(version);
				database.createDataUpdate(_id, _version, _date, _direct);
				database.close();
				
				arrUpdate.add(update);
			}
		} catch (Exception e) {
			
		}
		return null;
	}

	@Override
	protected void onPostExecute(ArrayList<DocPojo> result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		Toast.makeText(activity, "Đang tải dữ liệu!!!", Toast.LENGTH_LONG)
				.show();

		//activity.setUpdate(arrUpdate);
	}
}
